package com.app.shop.demo.adapters;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.app.shop.demo.MyApplication;
import com.app.shop.demo.R;
import com.app.shop.demo.beans.GoodsBean;
import com.app.shop.mylibrary.base.CommonAdapter;
import com.app.shop.mylibrary.base.ViewHolder;
import com.app.shop.mylibrary.beans.EventMessage;
import com.app.shop.mylibrary.interfaces.I_NumChangeListener;
import com.app.shop.mylibrary.widgts.AddPlusView;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.ArrayList;

import de.greenrobot.event.EventBus;

/**
 * @anthor : w
 * @description :
 */


public class   ShopCarListAdapter extends CommonAdapter {
    public ShopCarListAdapter(Context context, ArrayList datas, int layoutId) {
        super(context, datas, layoutId);
    }

    @Override
    public void setView(ViewHolder holder, Object o, int position) {
        GoodsBean bean = (GoodsBean) o;
        SimpleDraweeView simpleDraweeView = holder.getView(R.id.imgv_list);
        TextView tv = holder.getView(R.id.tv_list_item);
        TextView tv_price = holder.getView(R.id.tv_price);
        TextView tv_remove = holder.getView(R.id.tv_remove);
        AddPlusView plusView = holder.getView(R.id.add_plus_num);

        plusView.setDefultNum(bean.getGoods_num());
        simpleDraweeView.setImageResource(bean.getGoods_pic());
        tv.setText(bean.getGoods_name());
        tv_price.setText("￥" + bean.getGoods_price());


        tv_remove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //如果购物车有该商品，则增加一条，如果没有则添加商品
                MyApplication.goodsBeanList.remove(bean);
                EventBus.getDefault().post(new EventMessage(EventMessage.REFRESH));
            }
        });

        plusView.setOnEditListener(new I_NumChangeListener() {
            @Override
            public void onNumAddListener(int num) {
                bean.setGoods_num(num);
                EventBus.getDefault().post(new EventMessage(EventMessage.REFRESH));
            }

            @Override
            public void onNumSubtractListener(int num) {
                bean.setGoods_num(num);
                EventBus.getDefault().post(new EventMessage(EventMessage.REFRESH));
            }
        });

    }
}
